#ifndef RTSPCTRL_H
#define RTSPCTRL_H

#include <QObject>
#include <opencv2/opencv.hpp>
#include <mutex>
#include <thread>
#include <condition_variable>
#include <QImage>

class QLabel;
class QTimer;

enum ThreadState
{
    IDLE_STATE=0,
    CACULATE_STATE
};

class RtspCtrl : public QObject
{
    Q_OBJECT
Q_SIGNALS:
    void signal_init_result(bool flag);
    void signal_update_image(const QImage&img);
public:
    explicit RtspCtrl(QObject *parent = nullptr);
    ~RtspCtrl();
    void InitRtsp(const QString&uri,QLabel* show_widget);
    void PlayRtsp();
    void StopRtsp();
    void GrabPic();
    bool GrapPic(const QString&pic_name);
private:
    Q_INVOKABLE void DoOpen(const QString& uri);
    void FinishedInit(bool ok);
    void InitThread();
private:
    cv::VideoCapture cap;
    QTimer *m_timer{};
    QLabel* m_show_wid{};
    std::mutex m_mutex;
    cv::Mat m_lastFrame;
    std::thread m_work_thread;
    ThreadState m_state{ThreadState::IDLE_STATE};
    std::condition_variable m_cv;
    std::mutex m_cv_mutex;
    bool m_exit{false};
    bool m_connected{false};
};

#endif // RTSPCTRL_H
